﻿/*
 * @Author: Suprewq
 * @Date: 2024-02-20 14:50:09
 * @LastEditTime: 2024-11-13 11:55:44
 * @FilePath:
 * /xict_ws/src/xcmg-intelligent-cockpit-three/src/xict_ui/apps/run_display_ui.cpp
 * /xict_ws/src/xcmg-intelligent-cockpit-three/src/xict_ui/apps/run_display_ui.cpp
 * /xict_ws/src/xcmg-intelligent-cockpit-three/src/xict_ui/apps/run_display_ui.cpp
 * @Description:
 * Copyright (c) 2024 by BDCA, All Rights Reserved.
 */
#include "xict_common/log/log.h"
#include "xict_common/utils/args.h"
#include "xict_ui_common/utils/config.h"
#include "xict_ui_core/canmanage.h"
#include "xict_ui_core/imageitem.h"
#include "xict_ui_core/imagemanage.h"
#include "xict_ui_core/paintitem.h"
#include "xict_ui_core/vehiclemanage.h"
#include "xict_ui_driver/utils/baseimgsdk.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickView>

int main(int argc, char* argv[]) {
    // QTextCodec::setCodecForLocale(QTextCodec::codecForUtfText("utf-8"));
    eCAL::Initialize(argc, argv, "xict_ui");
    eCAL::Util::EnableLoopback(true);
    eCAL::Process::SetState(proc_sev_healthy, proc_sev_level1, "I feel good !");

    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QCoreApplication::setOrganizationName("xcmg organization");

    QGuiApplication app(argc, argv);

    args::ArgumentParser parser("xict ui");
    args::HelpFlag flag_help(parser, "help", "Display this help menu",
                             {'h', "help"});
    args::ValueFlag<std::string> flag_config_file(
        parser, "config_file", "Path to config file", {"config_file", "c"});
    try {
        parser.ParseCLI(argc, argv);
    } catch (const args::Completion& e) {
        AERROR_F("Failed to complete the command line arguments: {}", e.what());
        return 0;
    } catch (const args::Help&) {
        std::cout << parser;
        return 0;
    } catch (const args::ParseError& e) {
        AERROR_F("Failed to parse the command line arguments: {}", e.what());
        AERROR << parser;
        return 1;
    }
    QString config_file = QString::fromStdString(args::get(flag_config_file));

    qmlRegisterType<xict_ui_core::ImageItem>("ImageItem", 1, 0, "ImageItem");
    // qmlRegisterType<PaintItem>("PaintItem", 1, 0, "PaintItem");

    QQmlApplicationEngine engine;

    xict_ui_common::Config::GetInstance().Init(config_file);
    xict_ui_core::VehicleManage* vehcile_manager =
        xict_ui_core::VehicleManage::getInstance();
    vehcile_manager->init(config_file);

    engine.rootContext()->setContextProperty("vehicle_manager",
                                             vehcile_manager);
    engine.rootContext()->setContextProperty("can_manager",
                                             new xict_ui_core::CanManager);
    engine.rootContext()->setContextProperty("image_manager",
                                             new xict_ui_core::ImageManager);

    const QUrl main_url(QStringLiteral("qrc:/main.qml"));

    QObject::connect(
        &engine, &QQmlApplicationEngine::objectCreated, &app,
        [main_url](QObject* obj, const QUrl& objUrl) {
            if (!obj && main_url == objUrl)
                QCoreApplication::exit();
        },
        Qt::QueuedConnection);

    engine.load(main_url);

    return app.exec();
}
